//
//  CLLocationManager.m
//  suRead
//
//  Created by colin on 2020/1/5.
//  Copyright © 2020 bao. All rights reserved.
//

#import "COLocationManager.h"

@interface COLocationManager()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property(nonatomic,copy)void(^locationSuccess)(CLLocationCoordinate2D location);
@end

@implementation COLocationManager

- (void)location:(void (^)(CLLocationCoordinate2D))calculateBlock {
    
    self.locationSuccess = calculateBlock;
    
    // 开始定位
    [_locationManager startUpdatingLocation];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initLocation];
    }
    return self;
}

- (void)initLocation
{
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    // 设置定位精度
    [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    // 请求用户权限：
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
       // 只在前台开启定位
        [_locationManager requestWhenInUseAuthorization];
                 // 在后台也可定位
        //        [_locationManager requestAlwaysAuthorization];
    }
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
        {
            if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                [self.locationManager requestAlwaysAuthorization];
            }
            NSLog(@"用户还未决定授权");
            break;
        }
        case kCLAuthorizationStatusRestricted:
        {
            NSLog(@"访问受限");
            // 通过 ip 获取位置信息
            break;
        }
        case kCLAuthorizationStatusDenied:
        {
            // 类方法，判断是否开启定位服务
            if ([CLLocationManager locationServicesEnabled]) {
                NSLog(@"定位服务开启，被拒绝");
                
            } else {
                NSLog(@"定位服务关闭，不可用");
            }
            break;
        }
        case kCLAuthorizationStatusAuthorizedAlways:
        {
//            NSLog(@"获得前后台授权");
            break;
        }
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        {
//            NSLog(@"获得前台授权");
            break;
        }
        default:
            break;
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [_locationManager stopUpdatingLocation];

    if (locations && [locations count] > 0) {
        CLLocation *newLocation = locations[0];
        CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
        if (self.locationSuccess) {
            self.locationSuccess(oldCoordinate);
            self.locationSuccess = nil;
        }
//        CLGeocoder *geocoder = [[CLGeocoder alloc]init];
//        [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//            if (placemarks && [placemarks count] > 0) {
//                // 纬度
//                CLLocationDegrees latitude = newLocation.coordinate.latitude;
//                // 经度
//                CLLocationDegrees longitude = newLocation.coordinate.longitude;
//                NSString *cityName = @"火星";
//                for (CLPlacemark *place in placemarks) {
//                    if (place.locality.length > 0) {
//                        cityName = place.locality;
//                    }
//                }
//            }
//        }];
    }
}

- (void)dealloc {
    [_locationManager stopUpdatingLocation];
}

@end
